package com.freshfresh.activity.home;

import java.util.HashMap;
import java.util.Map;

import android.app.Dialog;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Vibrator;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.freshfresh.activity.BaseActivity;
import com.freshfresh.activity.R;
import com.freshfresh.utils.FreshConstants;
import com.freshfresh.utils.ShakeListener;
import com.freshfresh.utils.UrlConstants;
import com.freshfresh.utils.Utils;
import com.freshfresh.view.AlertDialog;

public class ShakeActivity extends BaseActivity {

	// 摇一摇
	private Vibrator mVibrator;// 振动器
	private ShakeListener mShakeListener = null;
	private ImageView ib_tuichu;
	private static final String TAG = "TestSensorActivity";
	private static final int SENSOR_SHAKE = 10;
	// private boolean isFinal = true;
	private String userid;
	private String token;
	private Dialog pro;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.ac_shake);
		initView();
		// 初始化摇一摇监听事件
		initShake();
		// 手动震动器
		if (mVibrator == null) {
			mVibrator = (Vibrator) getApplication().getSystemService(
					VIBRATOR_SERVICE);
		}
	}

	private void initView() {
		ib_tuichu = (ImageView) findViewById(R.id.ib_tuichu);
		ib_tuichu.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});
	}

	/**
	 * 14.15. 领取优惠券
	 */
	private void request() {

		SharedPreferences sp = FreshConstants.getUserShared(ShakeActivity.this);
		userid = sp.getString("userid", "");
		token = sp.getString("token", "");
		if (userid == null) {
			// ToastUtil.showToast("亲，您还没登录哦，登录后再来摇金币吧");
			Toast.makeText(ShakeActivity.this, "你还没有登录~~", 1).show();
			// new AlertDialog(ShakeActivity.this).builder()
			// .setImageView(R.drawable.yaoyiyao_nologined)
			// .setMsg("亲，您还没登录哦！\n登录后再来摇金币吧~~", null, R.color.light_red)
			// .show();
			// return;
		}
		Map<String, String> params = new HashMap<String, String>();
		params.put("customerid", userid);
		params.put("token", token);
//		pro = Utils.createLoadingDialog(ShakeActivity.this, "正在获取优惠信息");
//		pro.show();

		executeRequest(new StringRequest(UrlConstants.shakePoints, params,
				new Response.Listener<String>() {

					@Override
					public void onResponse(String arg0) {
//						pro.dismiss();
						// isFinal = false;

						if (Utils.parseJsonStr(arg0).get("result").equals("1")) {
							Integer add_points = (Integer) Utils.parseJsonStr(arg0)
									.get("add_points");// 得到的积分
//							Integer add_points = Integer.valueOf(ii);

							Map<String, Object> map = (Map<String, Object>) Utils
									.parseJsonStr(arg0).get("points");
							Integer accumulated_point = (Integer) map.get("current_points");// 积分总数
//							Integer accumulated_point = Integer.valueOf(jj);
							if (add_points == null || add_points.SIZE == 0) {
								add_points = 0;
							}
							if (accumulated_point == null
									|| accumulated_point.SIZE == 0) {
								accumulated_point = 0;
							}
							new AlertDialog(ShakeActivity.this)
									.builder()
									.setImageView(R.drawable.yaoyiyao_geded)
									.setMsg("恭喜你，此次摇到了" + add_points + "积分！")
									.setMsg_small(
											"(您的总积分" + accumulated_point
													+ ",继续努力哦)",
											Gravity.CENTER_HORIZONTAL,
											R.color.black).show();

						} else {
							// 是的 这个是问题 现在约定result等于2 返回已经摇过
							String errorMsg = Utils.parseJsonStr(arg0)
									.get("errorcode").toString();
							if ("Is shaked today.".equals(errorMsg)) {
								new AlertDialog(ShakeActivity.this)
										.builder()
										.setImageView(
												R.drawable.yaoyiyao_yao_guo)
										.setMsg("亲，您今天已经摇过了!\n明天再来摇吧~~", null,
												R.color.light_red).show();
							} else {
								showToast(ShakeActivity.this, "您还没有登录~~");
							}
							return;

						}
					}

				}, new Response.ErrorListener() {

					@Override
					public void onErrorResponse(VolleyError arg0) {

					}
				}));
	}

	/**
	 * 初始化摇一摇监听事件
	 */
	private void initShake() {
		if (mShakeListener != null) {
			return;
		}
		mShakeListener = new ShakeListener(this);
		mShakeListener.setOnShakeListener(new ShakeListener.OnShakeListener() {
			public void onShake() {
				startVibrato();// 开始震动
				mShakeListener.stop();
				new Handler().postDelayed(new Runnable() {
					public void run() {
						mVibrator.cancel();
						mShakeListener.start();
						// 开始请求数据
						request();
					}
				}, 2000);
			}
		});
	}

	/**
	 * 开始振动
	 */
	public void startVibrato() { // 定义震动
		mVibrator.vibrate(new long[] { 500, 200, 500, 200 }, -1); // 第一个｛｝里面是节奏数组，
		// 第二个参数是重复次数，-1为不重复，非-1俄日从pattern的指定下标开始重复
	}
}
